﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp.Service
{
    public class HelpFunc
    {

        public static void GenerateRandomGps(double baseLog, double baseLat, double radius, out string loga, out string lata)
        {
            radius /= 111300; // 将半径转换为度  
            Random random = new Random();
            double u = random.NextDouble(); // 生成0到1之间的随机浮点数  
            double v = random.NextDouble();
            double w = radius * Math.Sqrt(u);
            double t = 2 * Math.PI * v;
            double x = w * Math.Cos(t);
            double y = w * Math.Sin(t);
            double longitude = y + baseLog;
            double latitude = x + baseLat;

            loga = longitude.ToString("F6"); // 保留6位小数  
            lata = latitude.ToString("F6");
        }

        public static string get_tosign(string planId,string userId,string address)
        {
            string type = "START";

            string s = "Android" + type + planId + userId + address + "3478cbbc33f84bd00d75d7dfa69e0daa";

            string tosign_sign = GetMd5Hash(s);

            return tosign_sign;
        }

        public static string getPlanByStuSign(string userId,string roleKey)
        {
            string secret = "3478cbbc33f84bd00d75d7dfa69e0daa";
            string s = userId + roleKey + secret;
            string sign = GetMd5Hash(s);
            return sign;
        }

        static string GetMd5Hash(string input)
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);
                byte[] hashBytes = md5.ComputeHash(inputBytes);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("x2"));
                }

                return sb.ToString();
            }
        }

        public static string JSON_SeleteNode(JToken json, string ReName)
        {
            try
            {
                string result = "";
                var node = json.SelectToken("$.." + ReName);
                if (node != null)
                {
                    //判断节点类型
                    if (node.Type == JTokenType.String || node.Type == JTokenType.Integer || node.Type == JTokenType.Float)
                    {
                        //返回string值
                        result = node.Value<object>().ToString();
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

    }
}
